package test_new;


public class SortArray {
	
	public static void main (String[] array) {
		String direction = array[0];
		array = subArray(array);
		int[] numbers = convert(array);
		IntComparator comparator = direction.contains("desc") ? new ReversOrderComparator() : new NaturalOrderComparator();
		if (direction.contains("mod")) {
			comparator = new AbsComparator(comparator);
		}

		BubbleSort arraySort = null;
		if (direction.endsWith("*")) {
			arraySort = new MergeSort(numbers, comparator);
		} else {
			arraySort = new BubbleSort(numbers, comparator);	
		}
		//TODO make common interface for sorters
		//TODO remove unused classes and methods
		//TODO implement MergeSort
		
		
		arraySort.makeBubbleSort();

		String result = HelloWorld.join(convert(arraySort.getArray()), " ");
		System.out.println(result);
	}

	private static String[] subArray(String[] array) {
		String[] result = new String[array.length - 1];
		for (int i = 1; i < array.length; i++) {
			result[i-1] = array[i];
		}
		return result;
	}

	private static String[] convert(int[] array) {
		String[] arrayInt = new String[array.length];
		for (int i = 0; i < array.length; i++) {
			arrayInt[i] = "" + array[i];
		}
		return arrayInt;
	}

	private static int[] convert(String[] array) {
		int[] arrayInt = new int[array.length];
		for (int i = 0; i < array.length; i++) {
			arrayInt[i] = Integer.parseInt(array[i]);
		}
		return arrayInt;
	}


}
